package com.cairenhui.trust.auto.task.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.StringUtils;

/**
 * 
 * @author Zhangjun
 *
 */
public class TrustPropertyConfigurer extends PropertyPlaceholderConfigurer {
	private static Map<String, Object> ctxPropertiesMap;
	public static final String FILE_SERVER = "fileServer";
	public static final String FILE_SERVER_PROTOCOL = "fileServerProtocol";
	public static final String FILE_SERVER_PORT = "fileServerPort";
	public static final String STORE_MAPPING = "storeMapping";
	public static final String SYS_RISK_EXAM = "sys.risk.exam";
	public static final String SYS_RISK_PAPER = "sys.risk.paper";

	@Override
	protected void processProperties(
			ConfigurableListableBeanFactory beanFactoryToProcess,
			Properties props) throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		// 将资源文件键值对放入Map中
		ctxPropertiesMap = new HashMap<String, Object>();
		for (Object key : props.keySet()) {
			String keyStr = key.toString();
			String value = props.getProperty(keyStr);
			ctxPropertiesMap.put(keyStr, value);
		}
	}

	/**
	 * 获取上下文属性值
	 * 
	 * @param name
	 *            属性键名
	 * @return
	 */
	public static Object getContextProperty(String name) {
		return ctxPropertiesMap.get(name);
	}

	/**
	 * 获取文件服务器地址
	 * 
	 * @return
	 */
	public static String getFileServerURL() {
		StringBuilder sb = new StringBuilder();
		String protocol = (String) getContextProperty(FILE_SERVER_PROTOCOL);
		String server = (String) getContextProperty(FILE_SERVER);
		String serverPort = (String) getContextProperty(FILE_SERVER_PORT);
		String store = (String) TrustPropertyConfigurer
				.getContextProperty(STORE_MAPPING);
		if (StringUtils.isEmpty(protocol)) {
			protocol = "http";
		}
		if (StringUtils.isEmpty(server)) {
			server = "localhost";
		}
		if (StringUtils.isEmpty(store)) {
			store = "store";
		}

		sb.append(protocol);
		sb.append("://");
		sb.append(server);
		if (isNotDefaultPort(protocol, serverPort)) {
			sb.append(":");
			sb.append(serverPort);
		}
		sb.append("/");
		sb.append(store);
		sb.append("/");
		return sb.toString();
	}

	private static boolean isNotDefaultPort(String protocol, String serverPort) {
		boolean res = true;
		if (protocol.equalsIgnoreCase("http") && serverPort.equals("80")) {
			res = false;
		}
		if (protocol.equalsIgnoreCase("https") && serverPort.equals("443")) {
			res = false;
		}
		if (protocol.equalsIgnoreCase("ftp") && serverPort.equals("21")) {
			res = false;
		}
		return res;
	}

	/**
	 * 是否需要进行风险测评
	 * 
	 * @return
	 */
	public static boolean needRiskExam() {
		String needRiskExam = (String) getContextProperty(SYS_RISK_EXAM);
		if (StringUtils.isEmpty(needRiskExam)
				|| "true".equalsIgnoreCase(needRiskExam)) {
			return true;
		}
		return false;
	}

	/**
	 * 是否需要签署风险揭示书
	 * 
	 * @return
	 */
	public static boolean needRiskPaper() {
		String needRiskPaper = (String) getContextProperty(SYS_RISK_PAPER);
		if (StringUtils.isEmpty(needRiskPaper)
				|| "true".equalsIgnoreCase(needRiskPaper)) {
			return true;
		}
		return false;
	}
}
